package com.qinggeng.project.system.controller;

import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.DistributionRegion;
import com.qinggeng.project.system.domain.SysRegion;
import com.qinggeng.project.system.service.IDistributionRegionService;
import com.qinggeng.project.system.service.ISysRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地区配送Controller
 *
 * @author qinggeng
 * @date 2020-06-18
 */
@RestController
@RequestMapping("/system/region")
public class DistributionRegionController extends BaseController {
    @Autowired
    private IDistributionRegionService distributionRegionService;
    @Autowired
    private ISysRegionService sysRegionService;

    /**
     * 查询地区配送列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(DistributionRegion distributionRegion) {
        startPage();
        List<DistributionRegion> list = distributionRegionService.selectDistributionRegionList(distributionRegion);
        return getDataTable(list);
    }

    /**
     * 导出地区配送列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:export')")
    @Log(title = "地区配送", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DistributionRegion distributionRegion) {
        List<DistributionRegion> list = distributionRegionService.selectDistributionRegionList(distributionRegion);
        ExcelUtil<DistributionRegion> util = new ExcelUtil<DistributionRegion>(DistributionRegion.class);
        return util.exportExcel(list, "region");
    }

    /**
     * 获取地区配送详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(distributionRegionService.selectDistributionRegionById(id));
    }

    /**
     * 新增地区配送
     */
    @PreAuthorize("@ss.hasPermi('system:region:add')")
    @Log(title = "地区配送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DistributionRegion distributionRegion) {
        //获取作用级别  1 省  2 市 3 区
        SysRegion sysRegion = sysRegionService.selectSysRegionById(Long.parseLong(distributionRegion.getProvince()));
        distributionRegion.setProvince(sysRegion.getName());
        distributionRegion.setProvinceCode(sysRegion.getZip());
        sysRegion = sysRegionService.selectSysRegionById(Long.parseLong(distributionRegion.getCity()));
        distributionRegion.setCity(sysRegion.getName());
        distributionRegion.setCityCode(sysRegion.getZip());
        sysRegion = sysRegionService.selectSysRegionById(Long.parseLong(distributionRegion.getArea()));
        distributionRegion.setArea(sysRegion.getName());
        distributionRegion.setAreaCode(sysRegion.getZip());
        return toAjax(distributionRegionService.insertDistributionRegion(distributionRegion));
    }

    /**
     * 修改地区配送
     */
    @PreAuthorize("@ss.hasPermi('system:region:edit')")
    @Log(title = "地区配送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DistributionRegion distributionRegion) {
        return toAjax(distributionRegionService.updateDistributionRegion(distributionRegion));
    }

    /**
     * 删除地区配送
     */
    @PreAuthorize("@ss.hasPermi('system:region:remove')")
    @Log(title = "地区配送", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(distributionRegionService.deleteDistributionRegionByIds(ids));
    }
}
